package com.ccn.ui.a1ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
 * 1.2.3Canvas高阶使用技巧-变换，状态保存，离屏缓冲，粒子特效
 *
 * 平移操作 translate
 * 缩放操作 scale
 * 旋转 rotate
 * 拉伸（倾斜） skew
 * 切割 clip
 * 矩阵 Matrix
 */
public class A1_2_3TransformView extends View {
    private Paint mPaint;

    public A1_2_3TransformView(Context context) {
        this(context, null);
    }

    public A1_2_3TransformView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public A1_2_3TransformView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(4);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //1.平移操作
//        canvas.drawRect(0, 0, 400, 400, mPaint);
//        canvas.translate(50, 50);
//        mPaint.setColor(Color.GRAY);
//        canvas.drawRect(0, 0, 400, 400, mPaint);
//        canvas.drawLine(0, 0, 600, 600, mPaint);

        //2.缩放操作
        /*canvas.drawRect(200,200,700,700,mPaint);
//        canvas.scale(0.5f,0.5f);
        //先translate操作 px，py，再scale操作，再反向translate回原先位置
        canvas.scale(0.5f,0.5f,200,200);
        //上述代码等于下面三行代码
        canvas.translate(200,200);
        canvas.scale(0.5f,0.5f);
        canvas.translate(-200,-200);

        mPaint.setColor(Color.GRAY);
        canvas.drawRect(200,200,700,700,mPaint);*/

        //3.旋转操作
//        canvas.translate(50, 50);
//        canvas.drawRect(0,0,700,700,mPaint);
//        canvas.rotate(45);
//        mPaint.setColor(Color.GRAY);
//        canvas.drawRect(0,0,700,700,mPaint);

//        canvas.drawRect(400,400,900,900,mPaint);
//        canvas.rotate(45,650,650);//px py表示旋转中心的坐标
//        mPaint.setColor(Color.GRAY);
//        canvas.drawRect(400,400,900,900,mPaint);

        //4.倾斜操作
//        canvas.drawRect(0, 0, 400, 400, mPaint);
//        canvas.skew(1, 0);//表示x方向上倾斜45度，即y轴逆时针旋转45度
//        canvas.skew(0, 1);//表示y方向上倾斜45度，即x轴顺时针旋转45度
//        mPaint.setColor(Color.GRAY);
//        canvas.drawRect(0, 0, 400, 400, mPaint);

        //5.切割
//        canvas.drawRect(200, 200, 700, 700, mPaint);
//        mPaint.setColor(Color.GRAY);
//        canvas.drawRect(200, 800, 700, 1300, mPaint);
//        canvas.clipRect(200, 200, 700, 700);//画布被裁剪，有效区域（200,200）-（700,700）
//        canvas.drawCircle(100, 100, 100, mPaint);//坐标超出裁剪区域，无法绘制
//        canvas.drawCircle(300, 300, 100, mPaint);//坐标在裁剪区域，绘制成功

//        canvas.drawRect(200, 200, 700, 700, mPaint);
//        mPaint.setColor(Color.GRAY);
//        canvas.drawRect(200, 800, 700, 1300, mPaint);
//        canvas.clipOutRect(200, 200, 700, 700);//画布裁剪外的区域有效
//        canvas.drawCircle(100, 100, 100, mPaint);//坐标在裁剪区域，绘制成功
//        canvas.drawCircle(300, 300, 100, mPaint);//坐标超出裁剪区域，无法绘制

        //6.矩阵
        canvas.drawRect(0,0,700,700,mPaint);
        Matrix matrix = new Matrix();
        matrix.setTranslate(50,50);
        matrix.setRotate(45);//有覆盖操作
        matrix.setScale(0.5f,0.5f);//有覆盖操作
        canvas.setMatrix(matrix);
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(0,0,700,700,mPaint);

    }
}
